package com.fulong.service.impl;

import com.fulong.entity.DeviceOrgInfoEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.Query;

import com.fulong.dao.DeviceStatusInfoDao;
import com.fulong.entity.DeviceStatusInfoEntity;
import com.fulong.service.DeviceStatusInfoService;


@Service("deviceStatusInfoService")
public class DeviceStatusInfoServiceImpl extends ServiceImpl<DeviceStatusInfoDao, DeviceStatusInfoEntity> implements DeviceStatusInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //分页问题
        int currentpage  = 1 ;
        if(params.get("page")!=null) {
            currentpage = Integer.parseInt(params.get("page").toString());
            params.remove("page") ;
        }
        HashMap<String , Object> map = new HashMap<>();
        IPage<DeviceStatusInfoEntity> page = this.page(
                new Query<DeviceStatusInfoEntity>().getPage(params),
                new QueryWrapper<DeviceStatusInfoEntity>()
        );

        return new PageUtils(page);
    }



    @Override
    public List<DeviceStatusInfoEntity> withTree() {
        List<DeviceStatusInfoEntity> deviceStatusInfoEntities = baseMapper.selectList(null);
        List<DeviceStatusInfoEntity> collect = deviceStatusInfoEntities.stream().filter(entiy -> entiy.getLevel() == 0)
                .map((el) -> {
                    el.setChildren(getChildren(el, deviceStatusInfoEntities));
                    return el;
                })
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<DeviceStatusInfoEntity> getoption(Integer statuscode) {
        DeviceStatusInfoEntity deviceStatusInfoEntity = this.getOne(new QueryWrapper<DeviceStatusInfoEntity>().eq("statuscode", statuscode));
        String id = deviceStatusInfoEntity.getId();
        List<DeviceStatusInfoEntity> list = this.list(new QueryWrapper<DeviceStatusInfoEntity>().eq("parent", id));
        return  list;
    }


    public List<DeviceStatusInfoEntity> getChildren(DeviceStatusInfoEntity parent ,List<DeviceStatusInfoEntity> all) {
        return all.stream().filter(entiy->entiy.getParent().equals(parent.getId()))
                .map((childrent)->{
                    childrent.setChildren(getChildren(childrent , all));
                    return childrent;
                }).collect(Collectors.toList());
    }

}